/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package models;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.Connexion;
import utils.TableModel;
/**
 *
 * @author juju
 */
public class ClientMapper {
    private Connexion _connexion;
    
    public ClientMapper() {
        try {
            this._connexion = new Connexion();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public Client find(int id, Client client) throws SQLException  {
        String requete = "SELECT ID_CLIENT, PRENOM, NOM, DATENAISSANCE FROM PROJET.CLIENT WHERE ID_CLIENT=" + id;
        this._connexion.setQuery(requete);
        ResultSet resultat = this._connexion.getResultSet();
        while(resultat.next()){
            client.setNom(resultat.getString("NOM"));
            client.setPrenom(resultat.getString("PRENOM"));
            client.setDateNaissance(resultat.getString("DATENAISSANCE"));
            client.setId(Integer.parseInt(resultat.getString("ID_CLIENT")));
        }
        return client;
    }
    
    public void save(Client client) {
        if(client.getId() == 0){
            this.add(client);
        } else {
           this.update(client);
        }
    }
    
    private void add(Client client) {
        try {
            String requete = "INSERT INTO PROJET.CLIENT VALUES(0,'" +
                    client.getPrenom() + "', '" + client.getNom()
                    + "', '" + client.getDateNaissance()+"')";
            this._connexion.setUpdate(requete);
        } catch (SQLException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void update(Client client) {
        try {
            String requete = "UPDATE PROJET.CLIENT SET NOM='" + client.getNom() 
                    +  "', PRENOM='" + client.getPrenom() + "', DATENAISSANCE='" + client.getDateNaissance() 
                    + "' WHERE ID_CLIENT=" + client.getId();
            this._connexion.setUpdate(requete);
        } catch (SQLException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public TableModel fetchAll() {
        String requete = "SELECT ID_CLIENT, PRENOM, NOM, DATENAISSANCE FROM PROJET.CLIENT";
        String[] titre = {"ID","Prénom","Nom","Date de naissance"};
        TableModel model = null;
        try {
            model = new TableModel(this._connexion,requete,titre);
            //model.getRowCount();
        } catch (SQLException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        return model;
    }
    
    
}
